package net.togogo.egouweb1238.core.controller.seller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javafx.scene.control.Pagination;
import net.togogo.egouweb1238.common.utils.PageController;
import net.togogo.egouweb1238.core.domain.product.Brand;
import net.togogo.egouweb1238.core.service.product.IBrandService;

/**
 * 品牌
 * @author stone
 *
 */
@Controller
@RequestMapping(value = "/brand")
public class BrandController {

	@Resource
	private IBrandService brandService;

	//品牌列表页面
	@RequestMapping(value = "/list")
	public String list(HttpSession session, String name, Integer isDisplay, Integer pageNo, String search,
			ModelMap model) {
		//参数
		Brand brand = new Brand();
		//判断传进来的名称是否为Null并且还要判断 是否为空串   blank  ""  "   "   emtpy  ""   "  "
		if (StringUtils.isNotBlank(name)) {
			brand.setName(name);
		}
		//是  不是
		if (null != isDisplay) {
			brand.setIsDisplay(isDisplay);
		} else {
			brand.setIsDisplay(1);
		}
		PageController pc = (PageController) session.getAttribute("pc");
		if (pc == null) {
			int totalRows = brandService.getBrandCount(brand);
			pc = new PageController(totalRows, 1);
			session.setAttribute("pc", pc);
		}

		if (pageNo != null) {
			pc.setCurrentPage(pageNo);
		} else {
			int totalRows = brandService.getBrandCount(brand);
			pc.setPageController(totalRows, 1);
		}
	

		List<Brand> brands = brandService.getBrandsWithPage(pc, brand);

		//返回到品牌列表页面时可以显示品牌信息
		model.addAttribute("brands", brands);//request.setAttribute
		//返回到品牌列表页面可以在查询条件输入框中显示上一次查询的品牌名称
		model.addAttribute("name", name);//request.setAttribute
		model.addAttribute("isDisplay", isDisplay);//request.setAttribute

		return "brand/list";
	}

	//跳转品牌添加页面
	@RequestMapping(value = "/toAdd")
	public String toAdd() {
		return "brand/add";
	}

	//添加品牌
	@RequestMapping(value = "/add")
	public String add(Brand brand) {
		//添加开始
		brandService.addBrand(brand);
		return "redirect:/brand/list.do";
	}

	//删除一个品牌
	@RequestMapping(value = "/delete")
	public String delete(Integer id, ModelMap model) {
		brandService.deleteBrandById(id);
		return "redirect:/brand/list.do";
	}

	//删除多个品牌
	@RequestMapping(value = "/deletes.do")
	public String deletes(Integer[] ids, ModelMap model) {
		brandService.deleteBrandByIds(ids);
		return "redirect:/brand/list.do";
	}

	//去修改页面
	@RequestMapping(value = "/toEdit")
	public String toEdit(Integer id, ModelMap model) {

		Brand brand = brandService.getBrandById(id);
		model.addAttribute("brand", brand);
		return "brand/edit";
	}

	//去修改页面
	@RequestMapping(value = "/edit")
	public String edit(Brand brand, ModelMap model) {
		brandService.updateBrand(brand);
		return "redirect:/brand/list.do";
	}
}
